package com.ruoyi.supplier.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supplier.domain.TbStoreShelf;
import com.ruoyi.supplier.service.ITbStoreShelfService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存盘点Controller
 * 
 * @author ruoyi
 * @date 2021-03-08
 */
@RestController
@RequestMapping("/supplier/storeShelf")
public class TbStoreShelfController extends BaseController
{
    @Autowired
    private ITbStoreShelfService tbStoreShelfService;

    /**
     * 查询库存盘点列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeShelf:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbStoreShelf tbStoreShelf)
    {
        startPage();
        List<TbStoreShelf> list = tbStoreShelfService.selectTbStoreShelfList(tbStoreShelf);
        return getDataTable(list);
    }

    /**
     * 导出库存盘点列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeShelf:export')")
    @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbStoreShelf tbStoreShelf)
    {
        List<TbStoreShelf> list = tbStoreShelfService.selectTbStoreShelfList(tbStoreShelf);
        ExcelUtil<TbStoreShelf> util = new ExcelUtil<TbStoreShelf>(TbStoreShelf.class);
        return util.exportExcel(list, "storeShelf");
    }

    /**
     * 获取库存盘点详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeShelf:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbStoreShelfService.selectTbStoreShelfById(id));
    }

    /**
     * 新增库存盘点
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeShelf:add')")
    @Log(title = "库存盘点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbStoreShelf tbStoreShelf)
    {
        String username = SecurityUtils.getUsername();
        tbStoreShelf.setCreateBy(username);
        tbStoreShelf.setUpdateBy(username);
        // 如果状态等于 1 表示 需要将商品入库及修改数量
        if(tbStoreShelf.getStatus()==1){
            return toAjax(tbStoreShelfService.insertTbStoreShelfQty(tbStoreShelf));
        }
        return toAjax(tbStoreShelfService.insertTbStoreShelf(tbStoreShelf));
    }

    /**
     * 修改库存盘点
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeShelf:edit')")
    @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbStoreShelf tbStoreShelf)
    {
        String username = SecurityUtils.getUsername();
        tbStoreShelf.setUpdateBy(username);
        // 如果状态等于 1 表示 需要将商品入库及修改数量
        if(tbStoreShelf.getStatus()==1){
            return toAjax(tbStoreShelfService.updateTbStoreShelfQty(tbStoreShelf));
        }
        return toAjax(tbStoreShelfService.updateTbStoreShelf(tbStoreShelf));
    }

    /**
     * 删除库存盘点
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeShelf:remove')")
    @Log(title = "库存盘点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbStoreShelfService.deleteTbStoreShelfByIds(ids));
    }
}
